
<!DOCTYPE HTML>
<html lang="zh-hans" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>3.1.2 异步机制 · Ebook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        <meta name="author" content="zq_sdk@163.com">
        
        
    
    <link rel="stylesheet" href="../../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-disqus/plugin.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-tree/plugin.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-splitter/splitter.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-local-video/video-js.min.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-tbfed-pagefooter/footer.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-ace/ace.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-katex/katex.min.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="important.html" />
    
    
    <link rel="prev" href="core.html" />
    

    <style>
    @media only screen and (max-width: 640px) {
        .book-header .hidden-mobile {
            display: none;
        }
    }
    </style>
    <script>
        window["gitbook-plugin-github-buttons"] = {"repo":"TIME-GATE/","types":["star","watch"],"size":"large"};
    </script>

    
    <link rel="stylesheet" href="../../gitbook/gitbook-plugin-chart/c3/c3.min.css">
    <script src="../../gitbook/gitbook-plugin-chart/c3/d3.min.js"></script>
    <script src="../../gitbook/gitbook-plugin-chart/c3/c3.min.js"></script>
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="输入并搜索" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../../">
            
                <a href="../../">
            
                    
                    简介
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1.1" data-path="../../about_me.html">
            
                <a href="../../about_me.html">
            
                    
                    关于
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.2" data-path="../../gitbook_install.html">
            
                <a href="../../gitbook_install.html">
            
                    
                    附:Gitbook安装
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../../">
            
                <a href="../../">
            
                    
                    一 基础理论
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="../../data_structure/">
            
                <a href="../../data_structure/">
            
                    
                    1 数据结构
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1.1" data-path="../../data_structure/11_linear_structure.html">
            
                <a href="../../data_structure/11_linear_structure.html">
            
                    
                    1.1 数组、链表、跳表及字典
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1.1.1" data-path="../../data_structure/111_array.html">
            
                <a href="../../data_structure/111_array.html">
            
                    
                    1.1.1 数组
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.1.1.2" data-path="../../data_structure/112_link_list.html">
            
                <a href="../../data_structure/112_link_list.html">
            
                    
                    1.1.2 链表
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.1.1.3" data-path="../../data_structure/113_skip_list.html">
            
                <a href="../../data_structure/113_skip_list.html">
            
                    
                    1.1.3 跳表
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.1.1.4" data-path="../../data_structure/114_dictionary.html">
            
                <a href="../../data_structure/114_dictionary.html">
            
                    
                    1.1.4 字典
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.2.1.2" data-path="../../data_structure/12_stack_and_queue.html">
            
                <a href="../../data_structure/12_stack_and_queue.html">
            
                    
                    1.2 栈、队列、散列及集合
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1.2.1" data-path="../../data_structure/121_stack.html">
            
                <a href="../../data_structure/121_stack.html">
            
                    
                    1.2.1 栈
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.1.2.2" data-path="../../data_structure/122_queue.html">
            
                <a href="../../data_structure/122_queue.html">
            
                    
                    1.2.2 队列
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.1.2.3" data-path="../../data_structure/123_hash.html">
            
                <a href="../../data_structure/123_hash.html">
            
                    
                    1.2.3 散列
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.1.2.4" data-path="../../data_structure/124_set.html">
            
                <a href="../../data_structure/124_set.html">
            
                    
                    1.2.4 集合
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.2.1.3" data-path="../../data_structure/13_tree_and_forest.html">
            
                <a href="../../data_structure/13_tree_and_forest.html">
            
                    
                    1.3 树与森林
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1.3.1" data-path="../../data_structure/131.html">
            
                <a href="../../data_structure/131.html">
            
                    
                    1.3.1 二叉搜索树
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.1.3.2" data-path="../../data_structure/132.html">
            
                <a href="../../data_structure/132.html">
            
                    
                    1.3.2 红黑树
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.2.1.4" data-path="../../data_structure/14_map.html">
            
                <a href="../../data_structure/14_map.html">
            
                    
                    1.4 图结构
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1.4.1" data-path="../../data_structure/141_minimal_tree.html">
            
                <a href="../../data_structure/141_minimal_tree.html">
            
                    
                    1.4.1 最小生成树
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.1.4.2" data-path="../../data_structure/142_single_source_shortest_path.html">
            
                <a href="../../data_structure/142_single_source_shortest_path.html">
            
                    
                    1.4.2 单源最短路径
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.1.4.3" data-path="../../data_structure/143_all_source_shortest_path.html">
            
                <a href="../../data_structure/143_all_source_shortest_path.html">
            
                    
                    1.4.3 多源最短路径
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.1.4.4" data-path="../../data_structure/144_max_stream.html">
            
                <a href="../../data_structure/144_max_stream.html">
            
                    
                    1.4.4 最大流
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="../../">
            
                <a href="../../">
            
                    
                    2 基础算法
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.2.1" data-path="../../algorithm/21.html">
            
                <a href="../../algorithm/21.html">
            
                    
                    2.1 排序查找
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2.2" data-path="../../algorithm/22.html">
            
                <a href="../../algorithm/22.html">
            
                    
                    2.2 动态规划
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.2.3" data-path="../../algorithm/distributed_algorithm.html">
            
                <a href="../../algorithm/distributed_algorithm.html">
            
                    
                    3 分布式算法
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.3.1" data-path="../../engineering/ci_deplopment/cs/theory.html">
            
                <a href="../../engineering/ci_deplopment/cs/theory.html">
            
                    
                    3.1 一致性模式与理论
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3.2" data-path="../../algorithm/raft.html">
            
                <a href="../../algorithm/raft.html">
            
                    
                    3.2 Raft协议
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3.3" data-path="../../algorithm/paxos.html">
            
                <a href="../../algorithm/paxos.html">
            
                    
                    3.3 Paxos协议
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3.4" data-path="../../algorithm/gossip.html">
            
                <a href="../../algorithm/gossip.html">
            
                    
                    3.4 Gossip协议
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3.5" data-path="../../engineering/ci_deplopment/cs/plan.html">
            
                <a href="../../engineering/ci_deplopment/cs/plan.html">
            
                    
                    3.5 几种一致性方案
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.2.4" data-path="../../optimization_se/">
            
                <a href="../../optimization_se/">
            
                    
                    4 运筹及系统工程
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.4.1" data-path="../../optimization_se/linear_programming.html">
            
                <a href="../../optimization_se/linear_programming.html">
            
                    
                    4.1 线性规划
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.4.2" data-path="../../optimization_se/integer_programming.html">
            
                <a href="../../optimization_se/integer_programming.html">
            
                    
                    4.2 整数规划
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.4.3" data-path="../../optimization_se/目标规划.html">
            
                <a href="../../optimization_se/目标规划.html">
            
                    
                    4.3 目标规划
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.4.4" data-path="../../optimization_se/非线性规划.html">
            
                <a href="../../optimization_se/非线性规划.html">
            
                    
                    4.4 非线性规划
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.4.5" data-path="../../optimization_se/动态规划.html">
            
                <a href="../../optimization_se/动态规划.html">
            
                    
                    4.5 动态规划
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.4.6" data-path="../../optimization_se/排队论.html">
            
                <a href="../../optimization_se/排队论.html">
            
                    
                    4.6 排队论
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.4.7" data-path="../../optimization_se/博弈论.html">
            
                <a href="../../optimization_se/博弈论.html">
            
                    
                    4.7 博弈论
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" >
            
                <span>
            
                    
                    二 工程应用
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="../../engineering/design_pattern/">
            
                <a href="../../engineering/design_pattern/">
            
                    
                    1 设计模式
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1.1" data-path="../../engineering/design_pattern/strategy.html">
            
                <a href="../../engineering/design_pattern/strategy.html">
            
                    
                    1.1 策略模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.1.2" data-path="../../engineering/design_pattern/prototype.html">
            
                <a href="../../engineering/design_pattern/prototype.html">
            
                    
                    1.2 原型模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.1.3" data-path="../../engineering/design_pattern/singleton.html">
            
                <a href="../../engineering/design_pattern/singleton.html">
            
                    
                    1.3 单例模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.1.4" data-path="../../engineering/design_pattern/factory.html">
            
                <a href="../../engineering/design_pattern/factory.html">
            
                    
                    1.4 工厂模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.1.5" data-path="../../engineering/design_pattern/bridge.html">
            
                <a href="../../engineering/design_pattern/bridge.html">
            
                    
                    1.5 桥接模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.1.6" data-path="../../engineering/design_pattern/composite.html">
            
                <a href="../../engineering/design_pattern/composite.html">
            
                    
                    1.6 组合模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.1.7" data-path="../../engineering/design_pattern/facade.html">
            
                <a href="../../engineering/design_pattern/facade.html">
            
                    
                    1.7 门面模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.1.8" data-path="../../engineering/design_pattern/adapter.html">
            
                <a href="../../engineering/design_pattern/adapter.html">
            
                    
                    1.8 适配器模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.1.9" data-path="../../engineering/design_pattern/decorator.html">
            
                <a href="../../engineering/design_pattern/decorator.html">
            
                    
                    1.9 装饰者模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.1.10" data-path="../../engineering/design_pattern/flyweight.html">
            
                <a href="../../engineering/design_pattern/flyweight.html">
            
                    
                    1.10 享元模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.1.11" data-path="../../engineering/design_pattern/proxy.html">
            
                <a href="../../engineering/design_pattern/proxy.html">
            
                    
                    1.11 代理模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.1.12" data-path="../../engineering/design_pattern/middleware.html">
            
                <a href="../../engineering/design_pattern/middleware.html">
            
                    
                    1.12 中间件模式
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="../../engineering/network_prag/">
            
                <a href="../../engineering/network_prag/">
            
                    
                    2 网络编程
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.2.1" data-path="../../engineering/network_prag/socket.html">
            
                <a href="../../engineering/network_prag/socket.html">
            
                    
                    2.1 长连接
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2.2" data-path="../../engineering/network_prag/mq.html">
            
                <a href="../../engineering/network_prag/mq.html">
            
                    
                    2.2 MQ
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3.3" data-path="../">
            
                <a href="../">
            
                    
                    3 语言应用
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.3.1" data-path="node.html">
            
                <a href="node.html">
            
                    
                    3.1 Node.js
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.3.1.1" data-path="core.html">
            
                <a href="core.html">
            
                    
                    3.1.1 原理及构成
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.3.3.1.2" data-path="async.html">
            
                <a href="async.html">
            
                    
                    3.1.2 异步机制
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3.1.3" data-path="important.html">
            
                <a href="important.html">
            
                    
                    3.1.3 一些核心概念
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3.1.4" data-path="koa_web.html">
            
                <a href="koa_web.html">
            
                    
                    3.1.4 web应用: koa框架
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3.1.5" data-path="koa.html">
            
                <a href="koa.html">
            
                    
                    3.1.5 koa源码分析
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3.1.6" data-path="libuv.html">
            
                <a href="libuv.html">
            
                    
                    3.1.6 libuv网络库应用
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3.1.7" data-path="apm.html">
            
                <a href="apm.html">
            
                    
                    3.1.7 性能调优
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3.1.8" data-path="node_spider.html">
            
                <a href="node_spider.html">
            
                    
                    3.1.8 其他: Node爬虫
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3.1.9" data-path="node_map_reduce.html">
            
                <a href="node_map_reduce.html">
            
                    
                    3.1.9 其他: Node编写Mapreduce
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3.3.2" data-path="../c/c_and_plus.html">
            
                <a href="../c/c_and_plus.html">
            
                    
                    3.2 C/C++
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.3.2.1" data-path="../c/oop.html">
            
                <a href="../c/oop.html">
            
                    
                    3.2.1 面向对象
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3.2.2" data-path="../c/ipcs.html">
            
                <a href="../c/ipcs.html">
            
                    
                    3.2.2 进程通信
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3.2.3" data-path="../c/thread.html">
            
                <a href="../c/thread.html">
            
                    
                    3.2.3 线程管理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3.2.4" data-path="../c/memory.html">
            
                <a href="../c/memory.html">
            
                    
                    3.2.4 内存管理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3.2.5" data-path="../c/network.html">
            
                <a href="../c/network.html">
            
                    
                    3.2.5 网络编程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3.2.6" data-path="../c/build_tool.html">
            
                <a href="../c/build_tool.html">
            
                    
                    3.2.6 构建工具
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3.2.7" data-path="../c/thread_pool.html">
            
                <a href="../c/thread_pool.html">
            
                    
                    3.2.7 其他: 线程池
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

            </ul>
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="../../engineering/ci_deplopment/">
            
                <a href="../../engineering/ci_deplopment/">
            
                    
                    三 搭建部署
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="../../engineering/ci_deplopment/test.html">
            
                <a href="../../engineering/ci_deplopment/test.html">
            
                    
                    3.1 测试
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1.1" data-path="../../engineering/ci_deplopment/test/unit_testing.html">
            
                <a href="../../engineering/ci_deplopment/test/unit_testing.html">
            
                    
                    3.1.1 单元测试
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.1.2" data-path="../../engineering/ci_deplopment/test/stress_testing.html">
            
                <a href="../../engineering/ci_deplopment/test/stress_testing.html">
            
                    
                    3.1.2 压力测试
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="../../engineering/ci_deplopment/ci_readme.html">
            
                <a href="../../engineering/ci_deplopment/ci_readme.html">
            
                    
                    3.2 集成
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.2.1" data-path="../../engineering/ci_deplopment/ci/gitlab_ci.html">
            
                <a href="../../engineering/ci_deplopment/ci/gitlab_ci.html">
            
                    
                    3.2.1 GitLab CI
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2.2" data-path="../../engineering/ci_deplopment/ci/spinnaker.html">
            
                <a href="../../engineering/ci_deplopment/ci/spinnaker.html">
            
                    
                    3.2.2 Spinnaker
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="../../engineering/ci_deplopment/build.html">
            
                <a href="../../engineering/ci_deplopment/build.html">
            
                    
                    3.3 部署
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.3.1" data-path="../../engineering/ci_deplopment/k8s/k8s.html">
            
                <a href="../../engineering/ci_deplopment/k8s/k8s.html">
            
                    
                    3.3.1 k8s
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3.2" data-path="../../engineering/ci_deplopment/rancher/rancher.html">
            
                <a href="../../engineering/ci_deplopment/rancher/rancher.html">
            
                    
                    3.3.2 rancher
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3.3" data-path="../../engineering/ci_deplopment/swarm/swarm.html">
            
                <a href="../../engineering/ci_deplopment/swarm/swarm.html">
            
                    
                    3.3.3 swarm
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4.4" data-path="../../engineering/ci_deplopment/maintenance.html">
            
                <a href="../../engineering/ci_deplopment/maintenance.html">
            
                    
                    3.4 监控
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.4.1" data-path="../../engineering/ci_deplopment/monitor.html">
            
                <a href="../../engineering/ci_deplopment/monitor.html">
            
                    
                    3.4.1 容器监控
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.4.2" data-path="../../engineering/ci_deplopment/trace.html">
            
                <a href="../../engineering/ci_deplopment/trace.html">
            
                    
                    3.4.2 系统监控
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.4.3" data-path="../../engineering/ci_deplopment/app1.md">
            
                <span>
            
                    
                    3.4.2 应用监控
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4.5" data-path="../../engineering/ha/ha.html">
            
                <a href="../../engineering/ha/ha.html">
            
                    
                    3.5 高可用
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.5.1" data-path="../../engineering/ha/client.html">
            
                <a href="../../engineering/ha/client.html">
            
                    
                    3.5.1 接入层高可用
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.5.2" data-path="../../engineering/ha/service.html">
            
                <a href="../../engineering/ha/service.html">
            
                    
                    3.5.2 服务层高可用
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.5.3" data-path="../../engineering/ha/share.html">
            
                <a href="../../engineering/ha/share.html">
            
                    
                    3.5.3 存储层高可用
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4.6" data-path="../../engineering/ci_deplopment/hc/hc.html">
            
                <a href="../../engineering/ci_deplopment/hc/hc.html">
            
                    
                    3.6 高并发
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.6.1" data-path="../../engineering/ci_deplopment/hc/dns.html">
            
                <a href="../../engineering/ci_deplopment/hc/dns.html">
            
                    
                    3.6.1 基于DNS的负载均衡
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.6.2" data-path="../../engineering/ci_deplopment/hc/lvs.html">
            
                <a href="../../engineering/ci_deplopment/hc/lvs.html">
            
                    
                    3.6.2 基于Lvs的负载均衡
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.6.3" data-path="../../engineering/ci_deplopment/hc/haproxy.html">
            
                <a href="../../engineering/ci_deplopment/hc/haproxy.html">
            
                    
                    3.6.3 基于haproxy的负载均衡
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" >
            
                <span>
            
                    
                    四 架构模式
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" >
            
                <span>
            
                    
                    1 微服务架构设计与实践
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1.1" data-path="../java/spring-cloud.html">
            
                <a href="../java/spring-cloud.html">
            
                    
                    1.1 微服务概况
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.1.2" data-path="../java/spring-eureka.html">
            
                <a href="../java/spring-eureka.html">
            
                    
                    1.2 服务注册及发现
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.1.3" data-path="../java/spring-feign.html">
            
                <a href="../java/spring-feign.html">
            
                    
                    1.3 服务调用
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.1.4" data-path="../java/spring-zuul.html">
            
                <a href="../java/spring-zuul.html">
            
                    
                    1.4 网关及负载均衡
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.1.5" data-path="../java/spring-hystrix.html">
            
                <a href="../java/spring-hystrix.html">
            
                    
                    1.5 熔断、限流、扩缩容
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.1.6" data-path="../java/spring-config.html">
            
                <a href="../java/spring-config.html">
            
                    
                    1.6 云配置中心
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.1.7" data-path="../java/zipkin-tracing.html">
            
                <a href="../java/zipkin-tracing.html">
            
                    
                    1.7 分布式链路追踪
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.1.8" data-path="../java/swagger.html">
            
                <a href="../java/swagger.html">
            
                    
                    1.8 接口文档管理
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5.2" data-path="../../domain/">
            
                <a href="../../domain/">
            
                    
                    2 领域驱动模型
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.2.1" data-path="../../domain/domain.html">
            
                <a href="../../domain/domain.html">
            
                    
                    2.1 领域--Domain
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2.2" data-path="../../domain/entity.md">
            
                <span>
            
                    
                    2.2 领域实体--Entity
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2.3" data-path="../../domain/object.html">
            
                <a href="../../domain/object.html">
            
                    
                    2.3 值对象--Value Object
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2.4" data-path="../../domain/aggregate.html">
            
                <a href="../../domain/aggregate.html">
            
                    
                    2.4 聚合--Aggregate
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2.5" data-path="../../domain/service.html">
            
                <a href="../../domain/service.html">
            
                    
                    2.5 服务--Services
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2.6" data-path="../../domain/event.html">
            
                <a href="../../domain/event.html">
            
                    
                    2.6 领域事件--Event
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2.7" data-path="../../domain/framework.html">
            
                <a href="../../domain/framework.html">
            
                    
                    2.7 几种常见的DDD架构
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5.3" >
            
                <span>
            
                    
                    3 架构技术的演化与趋势
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.3.1" >
            
                <span>
            
                    
                    3.1 架构技术的发展历史及现状
            
                </span>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.3.2" >
            
                <span>
            
                    
                    3.2 Serverless无服务架构概念
            
                </span>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.3.3" >
            
                <span>
            
                    
                    3.3 ServerMesh网格服务概念
            
                </span>
            

            
        </li>
    

            </ul>
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.6" >
            
                <span>
            
                    
                    五 其他
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.6.1" data-path="../../story/talk.html">
            
                <a href="../../story/talk.html">
            
                    
                    5.1 小伙子,放松!
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            本书使用 GitBook 发布
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../.." >3.1.2 异步机制</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h3 id="&#x4E00;&#x3001;&#x5F02;&#x6B65;&#x57FA;&#x7840;">&#x4E00;&#x3001;&#x5F02;&#x6B65;&#x57FA;&#x7840;</h3>
<p>&#xA0;&#xA0;&#xA0;&#xA0;JS&#x4F5C;&#x4E3A;&#x5355;&#x7EBF;&#x7A0B;&#x8BED;&#x8A00;&#xFF0C;Event loop&#x6784;&#x6210;&#x4E86;&#x5176;&#x5F02;&#x6B65;&#x7684;&#x57FA;&#x7840;&#x3001;&#x5728;&#x6BCF;&#x4E00;&#x6B21;&#x4E8B;&#x4EF6;&#x5FAA;&#x73AF;&#x4E2D;&#xFF0C;&#x90FD;&#x5305;&#x542B;&#x4E24;&#x79CD;&#x7C7B;&#x578B;&#x4EFB;&#x52A1;&#x961F;&#x5217;:<code>macro-task</code>&#x548C;<code>micro-task</code>,&#x4E24;&#x8005;&#x4E4B;&#x95F4;&#x6709;&#x4EC0;&#x4E48;&#x533A;&#x522B;&#x548C;&#x8054;&#x7CFB;&#xFF1F;&#x53EF;&#x4EE5;&#x8FD9;&#x4E48;&#x8BF4;&#xFF0C;<code>macro-task</code>&#x5728;&#x4F5C;&#x4E3A;&#x6574;&#x4E2A;&#x4E3B;&#x7EBF;&#x4EFB;&#x52A1;&#x6267;&#x884C;&#x8FC7;&#x7A0B;&#x4E2D;&#xFF0C;Event loop&#x4F1A;&#x88AB;&#x5212;&#x5206;&#x4E3A;&#x591A;&#x4E2A;&#x9636;&#x6BB5;&#xFF0C;&#x800C;&#x5728;&#x67D0;&#x4E9B;&#x9636;&#x6BB5;&#x7ED3;&#x675F;&#x65F6;&#x53EF;&#x4EE5;&#x7A7F;&#x63D2;&#x5730;&#x6267;&#x884C;<code>micro-task</code>&#xFF0C;&#x800C;&#x5728;&#x6267;&#x884C;<code>micro-task</code>&#x65F6;&#xFF0C;&#x5219;&#x662F;&#x5168;&#x90E8;&#x6267;&#x884C;&#x5B8C;&#x624D;&#x8FDB;&#x884C;&#x4E0B;&#x4E00;&#x9636;&#x6BB5;&#x3002;</p>
<p><img src="http://cdn.find-world.top/node_async_1.png" alt="&#x6253;&#x5370;"></p>
<ul>
<li>1.1 ES7&#x51E0;&#x79CD;&#x5F02;&#x6B65;&#x65B9;&#x5F0F;</li>
</ul>
<p>&#xA0;&#xA0;&#xA0;&#xA0;&#x6574;&#x4E2A;js&#x4EE3;&#x7801;&#x5305;&#x542B;&#x4E86;&#x4E09;&#x90E8;&#x5206;&#xFF1A;&#x540C;&#x6B65;&#x4EE3;&#x7801;(JS&#x4E3B;&#x7EBF;&#x7A0B;)&#x3001;<code>macro-task</code>&#x3001;<code>micro-task</code>&#xFF0C;&#x5BF9;&#x8FD9;&#x53E5;&#x8BDD;&#x7684;&#x6B63;&#x786E;&#x7406;&#x89E3;&#x662F;:</p>
<pre><code>1&#x3001;JS&#x4E3B;&#x7EBF;&#x7A0B;&#x5148;&#x6267;&#x884C;&#xFF0C;&#x7136;&#x540E;&#x8FDB;&#x5165;&#x5230;`Event loop`&#xFF0C;
2&#x3001;&#x5982;&#x679C;&#x6B64;&#x65F6;&#x6709;`micro-task`&#x4F18;&#x5148;&#x6267;&#x884C;&#xFF0C;&#x5426;&#x5219;&#x8FDB;&#x5165; &#x4E0B;&#x4E00;&#x4E2A; macro-task&#xFF0C;&#x6CE8;&#x610F;&#xFF1A;micro-task&#x53EF;&#x80FD;&#x4F1A;&#x7A7F;&#x63D2;&#x5728;&#x6BCF;&#x4E2A;&#x9636;&#x6BB5;&#xFF0C;&#x4F46;&#x90FD;&#x662F;&#x4F18;&#x5148;&#x6267;&#x884C;&#xFF0C;
3&#x3001;&#x5FAA;&#x73AF;&#x6B65;&#x9AA4;1&#x3001;2
</code></pre><p>&#x51E0;&#x79CD;&#x5E38;&#x89C1;ES7&#x5F02;&#x6B65;&#x65B9;&#x5F0F;&#x8C03;&#x7528;&#x5982;&#x4E0B;:</p>
<pre><code>1&#x3001;&#x666E;&#x901A;callback
2&#x3001;process.nextTick&#x3001;promise
3&#x3001;setTimeout&#x3001;setInterval&#x3001;setImmediate
4&#x3001;async/await
5&#x3001;generator
6&#x3001;EventEmitter(Event&#x4E0E;Event loop&#x6CA1;&#x6709;&#x4EFB;&#x4F55;&#x5173;&#x7CFB;&#xFF0C;&#x662F;&#x540C;&#x6B65;&#x6267;&#x884C;&#xFF0C;&#x4E0D;&#x5C5E;&#x4E8E;&#x5F02;&#x6B65;&#xFF0C;&#x4F46;&#x901A;&#x5E38;&#x53EF;&#x4F5C;&#x4E3A;&#x5F02;&#x6B65;&#x6765;&#x7528;)
7&#x3001;&#x5176;&#x4ED6;&#xFF0C;&#x5982;&#x4E00;&#x4E9B;&#x94FE;&#x63A5;I/O&#x7B49;&#xFF0C;&#x6B64;&#x5904;&#x4E0D;&#x8BA8;&#x8BBA;
</code></pre><p>&#xA0;&#xA0;&#xA0;&#xA0;&#x9700;&#x8981;&#x6CE8;&#x610F;&#x7684;&#x662F;&#xFF0C;async/await&#x548C;generator&#x867D;&#x7136;&#x662F;Promise&#x7684;&#x8BED;&#x6CD5;&#x7CD6;&#xFF0C;&#x4F46;&#x4E0E;Promise&#x4EA6;&#x5B58;&#x5728;&#x4E00;&#x4E9B;&#x533A;&#x522B;&#xFF0C;await&#x5FC5;&#x987B;&#x7D27;&#x8DDF;async&#xFF0C;&#x5176;&#x540E;&#x53EF;&#x4EE5;&#x662F;&#x4E00;&#x4E2A;promise&#x5BF9;&#x8C61;&#xFF0C;&#x4E5F;&#x53EF;&#x4EE5;&#x662F;&#x666E;&#x901A;&#x7684;&#x51FD;&#x6570;&#x6216;&#x53D8;&#x91CF;&#xFF0C;&#x4F46;&#x5728;&#x540E;&#x8DDF;&#x975E;Promise&#x7684;&#x60C5;&#x666F;&#x4E0B;&#xFF0C;&#x4F1A;&#x88AB;&#x8F6C;&#x6210;&#x4E00;&#x4E2A;&#x7ACB;&#x5373;resolve&#x7684;Promise&#x5BF9;&#x8C61;&#xFF0C;&#x4ED6;&#x4EEC;&#x672C;&#x8D28;&#x4E0A;&#x90FD;&#x662F;&#x72B6;&#x6001;&#x673A;&#x3002;</p>
<ul>
<li>1.2 &#x5F02;&#x6B65;&#x4F18;&#x5148;&#x7EA7;</li>
</ul>
<p>&#xA0;&#xA0;&#xA0;&#xA0;process.nextTick&#x4E0E;promise&#x90FD;&#x5C5E;&#x4E8E;micro&#x4EFB;&#x52A1;&#xFF0C;&#x6267;&#x884C;&#x4F18;&#x5148;&#x7EA7;&#x9AD8;&#x4E8E;macro&#x4EFB;&#x52A1;&#xFF0C;&#x540C;&#x65F6;&#xFF0C;setTimeout&#x4E0E;setInterval&#x53EF;&#x4EE5;&#x5212;&#x5206;&#x4E3A;macro&#x7C7B;&#x578B;&#x7684;&#x5B9A;&#x65F6;&#x5668;&#xFF0C;&#x6267;&#x884C;&#x4F18;&#x5148;&#x7EA7;&#x9AD8;&#x4E8E;setImmediate&#xFF08;check&#x9636;&#x6BB5;&#x6267;&#x884C;&#xFF09;&#xFF0C;&#x800C;EventEmitter&#x7684;&#x672C;&#x8D28;&#x662F;&#x57FA;&#x4E8E;&#x89C2;&#x5BDF;&#x8005;&#x6A21;&#x5F0F;&#x5B9E;&#x73B0;&#x76D1;&#x542C;&#x5668;&#xFF0C;&#x548C;&#x5F02;&#x6B65;&#x6CA1;&#x6709;&#x4EFB;&#x4F55;&#x5173;&#x7CFB;&#xFF0C;&#x6D41;&#x7A0B;&#x4E0A;&#x662F;&#x540C;&#x6B65;&#x6267;&#x884C;&#x3002;</p>
<pre><code>process.nextTick -&gt; promise = async/await -&gt; setTimeout = setInterval -&gt; setImmediate
</code></pre><ul>
<li>1.3 &#x5F02;&#x6B65;&#x6B21;&#x5E8F;&#x7684;&#x9A8C;&#x8BC1;</li>
</ul>
<p>&#x5148;&#x770B;&#x4E0B;&#x9762;&#x7684;&#x4F8B;&#x5B50;&#xFF1A;</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> EventEmitter = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;events&apos;</span>)
<span class="hljs-keyword">const</span> myEmitter = <span class="hljs-keyword">new</span> EventEmitter()

myEmitter.on(<span class="hljs-string">&apos;event&apos;</span>, (item) =&gt; {
  <span class="hljs-built_in">console</span>.log(<span class="hljs-number">5</span>)  
})

<span class="hljs-keyword">const</span> printAwait = <span class="hljs-keyword">async</span> () =&gt; {
  <span class="hljs-keyword">await</span> <span class="hljs-built_in">console</span>.log(<span class="hljs-number">6</span>)
  <span class="hljs-keyword">await</span> <span class="hljs-built_in">console</span>.log(<span class="hljs-number">7</span>)
}

<span class="hljs-keyword">const</span> printPromise = <span class="hljs-keyword">async</span> () =&gt; {
  <span class="hljs-keyword">await</span> <span class="hljs-built_in">Promise</span>.resolve().then(() =&gt; <span class="hljs-built_in">console</span>.log(<span class="hljs-number">8</span>))
  <span class="hljs-keyword">await</span> <span class="hljs-built_in">Promise</span>.resolve().then(() =&gt; <span class="hljs-built_in">console</span>.log(<span class="hljs-number">9</span>))
}

setImmediate(() =&gt; {
  <span class="hljs-built_in">console</span>.log(<span class="hljs-number">4</span>)
}, <span class="hljs-number">0</span>)

setTimeout(() =&gt; {
  <span class="hljs-built_in">console</span>.log(<span class="hljs-number">1</span>)
}, <span class="hljs-number">0</span>)

<span class="hljs-built_in">Promise</span>.resolve().then(() =&gt; {
  <span class="hljs-built_in">console</span>.log(<span class="hljs-number">2</span>)
})

process.nextTick(() =&gt; {
  <span class="hljs-built_in">console</span>.log(<span class="hljs-number">3</span>)
})

printAwait()
printPromise()
myEmitter.emit(<span class="hljs-string">&apos;event&apos;</span>)
</code></pre>
<p>&#x6267;&#x884C;&#x7ED3;&#x679C;&#xFF1A;</p>
<pre><code>Dreamboad:dreamboad helloworld$ node program/nodejs/async.js
6
5
3
2
7
8
9
1
4
</code></pre><p>&#x5728;printAwait&#x51FD;&#x6570;&#x4E2D;&#x7EE7;&#x7EED;&#x6DFB;&#x52A0;&#x5982;&#x4E0B;</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> printAwait = <span class="hljs-keyword">async</span> () =&gt; {
  <span class="hljs-keyword">await</span> <span class="hljs-built_in">console</span>.log(<span class="hljs-number">6</span>)
  <span class="hljs-keyword">await</span> <span class="hljs-built_in">console</span>.log(<span class="hljs-number">7</span>)
  <span class="hljs-keyword">await</span> <span class="hljs-built_in">console</span>.log(<span class="hljs-number">10</span>)
  <span class="hljs-keyword">await</span> <span class="hljs-built_in">Promise</span>.resolve().then(() =&gt; <span class="hljs-built_in">console</span>.log(<span class="hljs-number">11</span>))
  <span class="hljs-keyword">await</span> <span class="hljs-built_in">Promise</span>.resolve().then(() =&gt; <span class="hljs-built_in">console</span>.log(<span class="hljs-number">12</span>))
}
</code></pre>
<p>&#x518D;&#x6B21;&#x6267;&#x884C;async.js&#x51FD;&#x6570;:</p>
<pre><code>Dreamboad:dreamboad helloworld$ node program/nodejs/async.js
6
5
3
2
7
8
10
11
9
12
1
4
</code></pre><p>&#x8FD9;&#x4E5F;&#x9A8C;&#x8BC1;&#x4E86;&#x6211;&#x4EEC;&#x4E4B;&#x524D;<code>1.2&#x5C0F;&#x8282;</code>&#x7ED9;&#x51FA;&#x7684;&#x4F18;&#x5148;&#x7EA7;&#x7ED3;&#x8BBA;&#xFF0C;&#x6B64;&#x65F6;&#x6211;&#x4EEC;&#x4E5F;&#x53EF;&#x4EE5;&#x53D1;&#x73B0;&#x4E00;&#x4E2A;&#x7EC6;&#x8282;&#xFF0C;&#x82E5;await&#x540E;&#x9762;&#x7D27;&#x8DDF;&#x7684;&#x4E0D;&#x662F;promise&#x5BF9;&#x8C61;&#xFF0C;&#x5F53;&#x7A0B;&#x5E8F;&#x6267;&#x884C;&#x5230;await&#x65F6;&#xFF0C;&#x867D;&#x7136;&#x4F1A;&#x4FDD;&#x5B58;&#x5F53;&#x524D;&#x7684;&#x6267;&#x884C;&#x8FDB;&#x5EA6;&#x5E76;<code>&#x7ACB;&#x5373;&#x8FD4;&#x56DE;</code>&#xFF0C;&#x4F46;&#x540C;&#x65F6;&#x7D27;&#x8DDF;&#x5176;&#x540E;&#x7684;<code>&#x540C;&#x6B65;&#x8BED;&#x53E5;</code>&#x4E5F;&#x4F1A;&#x7ACB;&#x5373;&#x6267;&#x884C;&#x3002;</p>
<h3 id="&#x4E8C;&#x3001;&#x4ECE;node&#x6E90;&#x7801;&#x5206;&#x6790;event-loop">&#x4E8C;&#x3001;&#x4ECE;node&#x6E90;&#x7801;&#x5206;&#x6790;event loop</h3>
<ul>
<li>2.1 &#x4F55;&#x65F6;&#x751F;&#x6210;macro-task?</li>
</ul>
<p>node&#x6E90;&#x7801;tools/js2c.py:</p>
<pre><code class="lang-py"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">main</span><span class="hljs-params">()</span>:</span>
  natives = sys.argv[<span class="hljs-number">1</span>]
  source_files = sys.argv[<span class="hljs-number">2</span>:] <span class="hljs-comment"># &#x52A0;&#x8F7D;js&#x6E90;&#x6587;&#x4EF6;</span>
  <span class="hljs-keyword">if</span> source_files[<span class="hljs-number">-2</span>] == <span class="hljs-string">&apos;-t&apos;</span>:
    <span class="hljs-keyword">global</span> TEMPLATE
    TEMPLATE = source_files[<span class="hljs-number">-1</span>]
    source_files = source_files[:<span class="hljs-number">-2</span>]
  JS2C(source_files, [natives])

<span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">&quot;__main__&quot;</span>:
  main()
</code></pre>
<p>&#x5176;&#x4E2D;JS2C&#x51FD;&#x6570;&#x5C06;&#x6574;&#x4E2A;script&#x4F5C;&#x4E3A;&#x4E00;&#x4E2A;macro&#x6765;&#x52A0;&#x8F7D;&#x7F16;&#x8BD1;&#xFF0C;</p>
<pre><code class="lang-py"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">JS2C</span><span class="hljs-params">(source, target)</span>:</span>
  modules = []
  consts = {}
  macros = {}
  macro_lines = []

  <span class="hljs-keyword">for</span> s <span class="hljs-keyword">in</span> source:
    <span class="hljs-keyword">if</span> (os.path.split(str(s))[<span class="hljs-number">1</span>]).endswith(<span class="hljs-string">&apos;macros.py&apos;</span>):
      macro_lines.extend(ReadLines(str(s)))
    <span class="hljs-keyword">else</span>:
      modules.append(s)

  <span class="hljs-comment"># Process input from all *macro.py files</span>
  (consts, macros) = ReadMacros(macro_lines)

  <span class="hljs-comment"># Build source code lines</span>
  definitions = []
  initializers = []
</code></pre>
<ul>
<li>2.2 &#x4F55;&#x65F6;&#x6267;&#x884C;micro?</li>
</ul>
<p>&#x5728;node.cc&#x4E0E;isolate.cc&#x4EE3;&#x7801;&#x4E2D;&#xFF0C;&#x5F53;micro&#x961F;&#x5217;&#x975E;&#x7A7A;&#x65F6;&#xFF0C;&#x8BBE;&#x7F6E;&#x5F53;&#x524D;&#x8FD0;&#x884C;&#x72B6;&#x6001;&#xFF0C;&#x5E76;&#x6309;&#x6B21;&#x5E8F;&#x8FD0;&#x884C;&#x5BF9;&#x5E94;&#x7684;&#x6240;&#x6709;&#x5DF2;&#x6CE8;&#x518C;&#x7684;&#x56DE;&#x8C03;&#x51FD;&#x6570;&#xFF1A;</p>
<pre><code class="lang-js"><span class="hljs-comment">/* 1 &#x8FD0;&#x884C;micto&#x4EFB;&#x52A1;&#x961F;&#x5217; */</span>
<span class="hljs-keyword">void</span> Isolate::RunMicrotasks() {
  <span class="hljs-comment">// Increase call depth to prevent recursive callbacks.</span>
  v8::Isolate::SuppressMicrotaskExecutionScope suppress(
      reinterpret_cast&lt;v8::Isolate*&gt;(<span class="hljs-keyword">this</span>));
  HandleScope scope(<span class="hljs-keyword">this</span>);
  Handle&lt;MicrotaskQueue&gt; microtask_queue(heap()-&gt;default_microtask_queue(),
                                         <span class="hljs-keyword">this</span>);
  <span class="hljs-keyword">if</span> (microtask_queue-&gt;pending_microtask_count()) {
    is_running_microtasks_ = <span class="hljs-literal">true</span>;
    TRACE_EVENT0(<span class="hljs-string">&quot;v8.execute&quot;</span>, <span class="hljs-string">&quot;RunMicrotasks&quot;</span>);
    TRACE_EVENT_CALL_STATS_SCOPED(<span class="hljs-keyword">this</span>, <span class="hljs-string">&quot;v8&quot;</span>, <span class="hljs-string">&quot;V8.RunMicrotasks&quot;</span>);

    MaybeHandle&lt;<span class="hljs-built_in">Object</span>&gt; maybe_exception;
    MaybeHandle&lt;<span class="hljs-built_in">Object</span>&gt; maybe_result = Execution::RunMicrotasks(
        <span class="hljs-keyword">this</span>, Execution::MessageHandling::kReport, &amp;maybe_exception);
    <span class="hljs-comment">// If execution is terminating, bail out, clean up, and propagate to</span>
    <span class="hljs-comment">// TryCatch scope.</span>
    <span class="hljs-keyword">if</span> (maybe_result.is_null() &amp;&amp; maybe_exception.is_null()) {
      microtask_queue-&gt;set_queue(ReadOnlyRoots(heap()).empty_fixed_array());
      microtask_queue-&gt;set_pending_microtask_count(<span class="hljs-number">0</span>);
      handle_scope_implementer()-&gt;LeaveMicrotaskContext();
      SetTerminationOnExternalTryCatch();
    }
    CHECK_EQ(<span class="hljs-number">0</span>, microtask_queue-&gt;pending_microtask_count());
    CHECK_EQ(<span class="hljs-number">0</span>, microtask_queue-&gt;queue()-&gt;length());
    is_running_microtasks_ = <span class="hljs-literal">false</span>;
  }
  FireMicrotasksCompletedCallback();
}

<span class="hljs-comment">/* 2&#x3001;&#x8FD0;&#x884C;&#x6240;&#x6709;&#x56DE;&#x8C03;&#x51FD;&#x6570;*/</span>
<span class="hljs-keyword">void</span> Isolate::FireMicrotasksCompletedCallback() {
  std::vector&lt;MicrotasksCompletedCallback&gt; callbacks(
      microtasks_completed_callbacks_);
  <span class="hljs-keyword">for</span> (auto&amp; callback : callbacks) {
    callback(reinterpret_cast&lt;v8::Isolate*&gt;(<span class="hljs-keyword">this</span>));
  }
}
</code></pre>
<p>micro&#x7684;&#x4EFB;&#x52A1;&#x6267;&#x884C;&#x7B56;&#x7565;&#x4E00;&#x5171;&#x4E09;&#x79CD;&#xFF0C;&#x9ED8;&#x8BA4;&#x662F;kScoped&#xFF0C;&#x4E09;&#x79CD;&#x7B56;&#x7565;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-js"><span class="hljs-comment">/**
 * Policy for running microtasks:
 *   - explicit: microtasks are invoked with Isolate::RunMicrotasks() method;
 *   - scoped: microtasks invocation is controlled by MicrotasksScope objects;
 *   - auto: microtasks are invoked when the script call depth decrements
 *           to zero.
 */</span>
enum <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MicrotasksPolicy</span> </span>{ kExplicit, kScoped, kAuto };
</code></pre>
<p>RunMicrotasks&#x4F1A;&#x4EE5;api&#x7684;&#x5F62;&#x5F0F;&#x63D0;&#x4F9B;js&#x8C03;&#x7528;&#xFF0C;&#x5728;api.cc&#x6587;&#x4EF6;&#x4E2D;:</p>
<pre><code class="lang-js"><span class="hljs-keyword">void</span> Isolate::RunMicrotasks() {
  DCHECK_NE(MicrotasksPolicy::kScoped, GetMicrotasksPolicy());
  reinterpret_cast&lt;i::Isolate*&gt;(<span class="hljs-keyword">this</span>)-&gt;RunMicrotasks();
}
</code></pre>
<p>&#x88AB;setupNextTick(lib/process/next_tick.js)&#x8C03;&#x7528;&#xFF1A;</p>
<pre><code class="lang-js">  <span class="hljs-comment">// tickInfo is used so that the C++ code in src/node.cc can</span>
  <span class="hljs-comment">// have easy access to our nextTick state, and avoid unnecessary</span>
  <span class="hljs-comment">// calls into JS land.</span>
  <span class="hljs-comment">// runMicrotasks is used to run V8&apos;s micro task queue.</span>
  <span class="hljs-keyword">const</span> [
    tickInfo,
    runMicrotasks
  ] = _setupNextTick(internalTickCallback);

  <span class="hljs-comment">// *Must* match Environment::TickInfo::Fields in src/env.h.</span>
  <span class="hljs-keyword">const</span> kHasScheduled = <span class="hljs-number">0</span>;
  <span class="hljs-keyword">const</span> kHasPromiseRejections = <span class="hljs-number">1</span>;

  <span class="hljs-keyword">const</span> queue = <span class="hljs-keyword">new</span> FixedQueue();

  process.nextTick = nextTick;
  <span class="hljs-comment">// Needs to be accessible from beyond this scope.</span>
  process._tickCallback = _tickCallback;

  <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">_tickCallback</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-keyword">if</span> (tickInfo[kHasScheduled] === <span class="hljs-number">0</span> &amp;&amp; tickInfo[kHasPromiseRejections] === <span class="hljs-number">0</span>)
      runMicrotasks();
    <span class="hljs-keyword">if</span> (tickInfo[kHasScheduled] === <span class="hljs-number">0</span> &amp;&amp; tickInfo[kHasPromiseRejections] === <span class="hljs-number">0</span>)
      <span class="hljs-keyword">return</span>;

    internalTickCallback();
  }

<span class="hljs-comment">/* 
 * &#x53EF;&#x4EE5;&#x770B;&#x5230; process.nextTick&#x4E0D;&#x5141;&#x8BB8;event loop&#x7EE7;&#x7EED;&#x6267;&#x884C;&#xFF0C;
 * &#x9664;&#x975E;Tick&#x961F;&#x5217;&#x7684;&#x6240;&#x6709;&#x56DE;&#x8C03;&#x51FD;&#x6570;&#x548C;micro&#x961F;&#x5217;&#x6267;&#x884C;&#x5B8C;&#x6210;&#xFF0C;&#x540C;&#x65F6;&#x4E5F;&#x8BF4;&#x660E;&#x4E3A;&#x4F55;Tick&#x4F1A;&#x5148;&#x4E0E;Promise&#x6267;&#x884C;, 
 */</span>
  <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">internalTickCallback</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-keyword">let</span> tock;
    <span class="hljs-keyword">do</span> {
      <span class="hljs-keyword">while</span> (tock = queue.shift()) {
        <span class="hljs-keyword">const</span> asyncId = tock[async_id_symbol];
        emitBefore(asyncId, tock[trigger_async_id_symbol]);
        <span class="hljs-comment">// emitDestroy() places the async_id_symbol into an asynchronous queue</span>
        <span class="hljs-comment">// that calls the destroy callback in the future. It&apos;s called before</span>
        <span class="hljs-comment">// calling tock.callback so destroy will be called even if the callback</span>
        <span class="hljs-comment">// throws an exception that is handled by &apos;uncaughtException&apos; or a</span>
        <span class="hljs-comment">// domain.</span>
        <span class="hljs-comment">// TODO(trevnorris): This is a bit of a hack. It relies on the fact</span>
        <span class="hljs-comment">// that nextTick() doesn&apos;t allow the event loop to proceed, but if</span>
        <span class="hljs-comment">// any async hooks are enabled during the callback&apos;s execution then</span>
        <span class="hljs-comment">// this tock&apos;s after hook will be called, but not its destroy hook.</span>
        <span class="hljs-keyword">if</span> (destroyHooksExist())
          emitDestroy(asyncId);

        <span class="hljs-keyword">const</span> callback = tock.callback;
        <span class="hljs-keyword">if</span> (tock.args === <span class="hljs-literal">undefined</span>)
          callback();
        <span class="hljs-keyword">else</span>
          <span class="hljs-built_in">Reflect</span>.apply(callback, <span class="hljs-literal">undefined</span>, tock.args);

        emitAfter(asyncId);
      }
      tickInfo[kHasScheduled] = <span class="hljs-number">0</span>;
      runMicrotasks();
    } <span class="hljs-keyword">while</span> (!queue.isEmpty() || emitPromiseRejectionWarnings());
    tickInfo[kHasPromiseRejections] = <span class="hljs-number">0</span>;
  }
</code></pre>
<h3 id="&#x4E09;&#x3001;&#x603B;&#x7ED3;">&#x4E09;&#x3001;&#x603B;&#x7ED3;</h3>
<p>&#xA0;&#xA0;&#xA0;&#xA0;&#x5B9E;&#x73B0;Node.js&#x5F02;&#x6B65;&#x7684;&#x65B9;&#x5F0F;&#x6709;&#x5F88;&#x591A;&#x79CD;&#xFF0C;&#x4F60;&#x4E5F;&#x8BB8;&#x5B8C;&#x5168;&#x53EF;&#x4EE5;&#x7528;async/await&#x6765;&#x5B9E;&#x73B0;&#x6240;&#x6709;&#x7684;&#x5F02;&#x6B65;&#x573A;&#x666F;&#xFF0C;&#x4F46;&#x5982;&#x679C;&#x4F60;&#x8FFD;&#x6C42;&#x66F4;&#x5BCC;&#x8868;&#x73B0;&#x529B;&#x7684;&#x65B9;&#x5F0F;&#x5B8C;&#x6210;&#x4EFB;&#x52A1;&#xFF0C;generator&#x3001;process.nextTick()&#x6709;&#x65F6;&#x4F1A;&#x662F;&#x66F4;&#x4F73;&#x7684;&#x7EC4;&#x5408;&#x3002;</p>
<ul>
<li><a href="https://nodejs.org/dist/latest-v9.x/docs/api/v8.html" target="_blank">1&#x3001;Node &#x6587;&#x6863;</a></li>
<li><a href="https://nodejs.org/en/docs/guides/event-loop-timers-and-nexttick/" target="_blank">2&#x3001;Node event loop</a></li>
<li><a href="https://github.com/nodejs/node/tree/master/src" target="_blank">3&#x3001;Node&#x6E90;&#x7801;</a></li>
<li><a href="http://docs.libuv.org/en/v1.x/design.html#the-i-o-loop" target="_blank">4&#x3001;libuv &#x5B98;&#x7F51;</a></li>
</ul>
<footer class="page-footer"><span class="copyright">Copyright &#xA9; &#x4E00;&#x84D1;&#x70DF;&#x96E8;&#x4EFB;&#x5E73;&#x751F; all right reserved&#xFF0C;powered by Gitbook</span><span class="footer-modification">&#x4FEE;&#x6539;&#x65F6;&#x95F4;: 
2021-04-09 11:09:02
</span></footer>
                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="core.html" class="navigation navigation-prev " aria-label="Previous page: 3.1.1 原理及构成">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="important.html" class="navigation navigation-next " aria-label="Next page: 3.1.3 一些核心概念">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"3.1.2 异步机制","level":"1.3.3.1.2","depth":4,"next":{"title":"3.1.3 一些核心概念","level":"1.3.3.1.3","depth":4,"path":"program/nodejs/important.md","ref":"program/nodejs/important.md","articles":[]},"previous":{"title":"3.1.1 原理及构成","level":"1.3.3.1.1","depth":4,"path":"program/nodejs/core.md","ref":"program/nodejs/core.md","articles":[]},"dir":"ltr"},"config":{"plugins":["disqus","tree","splitter","local-video","chart","github-buttons","tbfed-pagefooter","ace","katex"],"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"tbfed-pagefooter":{"copyright":"Copyright &copy 一蓑烟雨任平生","modify_label":"修改时间: ","modify_format":"YYYY-MM-DD HH:mm:ss"},"disqus":{"useIdentifier":false,"shortName":"gitbookuse"},"ace":{},"splitter":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"katex":{},"fontsettings":{"theme":"white","family":"sans","size":2},"highlight":{},"github-buttons":{"repo":"TIME-GATE/","types":["star","watch"],"size":"large"},"local-video":{},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false},"tree":{},"chart":{"type":"c3"}},"theme":"default","author":"zq_sdk@163.com","name":"name","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"Ebook","language":"zh-hans","gitbook":"*","description":"电子书"},"file":{"path":"program/nodejs/async.md","mtime":"2021-04-09T03:09:02.250Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2021-04-10T12:23:44.633Z"},"basePath":"../..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../../gitbook/gitbook.js"></script>
    <script src="../../gitbook/theme.js"></script>
    
        
        <script src="https://cdnjs.cloudflare.com/ajax/libs/URI.js/1.16.1/URI.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-disqus/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-tree/jquery.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-tree/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-splitter/splitter.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-local-video/video.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-github-buttons/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-ace/ace/ace.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-ace/ace.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

